package com.kaibes.platform.file.client.db;

import java.util.List;

import org.springframework.stereotype.Service;

import com.kaibes.orm.core.core.Page;
import com.kaibes.orm.curd.UpdateBuilder;
import com.kaibes.web.api.ResponseServiceImpl;

@Service
public class FileMapService extends ResponseServiceImpl<FileMap> {

    public Page<FileMap> pageByUuidLikeOrNameLike(int pageIndex, int pageSize, String search) {
        return page(pageIndex, pageSize, query().rightLike("uuid", search).build(), query().like("name", search).build());
    }
    
    public Page<FileMap> pageByType(int pageIndex, int pageSize, String type) {
        return page(pageIndex, pageSize, query().eq("type", type).build());
    }
    
    public Page<FileMap> pageByUserId(int pageIndex, int pageSize, Integer userId) {
        return page(pageIndex, pageSize, query().eq("user_id", userId).build());
    }
    
    public boolean removeByUserIdAndUuid(Integer userId, String uuid) {
        return remove(query().eq("user_id", userId).eq("uuid", uuid).build())>0;
    }
    
    public boolean removeByUuid(String uuid) {
        return remove(query().eq("uuid", uuid).build())>0;
    }
    
    // 个人
    public Page<FileMap> pageByUserIdAndUuidLikeOrNameLike(int pageIndex, int pageSize, Integer userId, String search) {
        return page(pageIndex, pageSize, query().eq("user_id", userId).rightLike("uuid", search).build(), query().eq("user_id", userId).like("name", search).build());
    }
    
    public Page<FileMap> pageByUserIdAndType(int pageIndex, int pageSize, Integer userId, String type) {
        return page(pageIndex, pageSize, query().eq("user_id", userId).eq("type", type).build());
    }
    
    public boolean removeByUserIdAndId(Integer userId, Number id) {
        return remove(query().eq("id", id).eq("user_id", userId).build())>0;
    }
    
    public boolean removeByUserIdAndIds(Integer userId, Number... ids) {
        return remove(query().inArray("id", ids).eq("user_id", userId).build())>0;
    }
    
    public boolean removeByUserIdAndIds(Integer userId, List<Long> ids) {
        return remove(query().inList("id", ids).eq("user_id", userId).build())>0;
    }
    
    public int updateTagByNull(String tag) {
        return getMapper().update(UpdateBuilder.newBuilder().setValue("tag", tag).addQuery(query().nullValue("tag").build()).build());
    }
    
    public int updateTagByTag(String tag, String old) {
        return getMapper().update(UpdateBuilder.newBuilder().setValue("tag", tag).addQuery(query().eq("tag", old).build()).build());
    }
    
    public Page<FileMap> pageByTag(int pageIndex, int pageSize, String tag) {
        return page(pageIndex, pageSize, query().eq("tag", tag).build());
    }
    
    public int removeByTags(List<String> tags) {
        return remove(query().inList("tag", tags).build());
    }
    
    public FileMap getByUuid(String uuid) {
    	return super.get(query().eq("uuid", uuid).build());
    }
    
    public long countByUserId(int userId) {
    	return super.count(query().eq("user_id", userId).build());
    }
    
}
